///////////////////////////////////////////////////
/*
Kevin DeLuca
Replication data for: "Editor's Choice: Measuring Candidate Quality Using Local Newspaper Endorsements", Journal of Politics, 2025
Main Analyses
*/
///////////////////////////////////////////////////

clear
set more off
set more off
graph drop _all

*set directory here
global mainPath = ""

cd "$mainPath"



**packages required for replication**
*ssc install reghdfe //both needed for estimation
*ssc install ftools //both needed for estimation
*ssc install outreg2 //needed for tables
*ssc install gtools //needed to run binscatter2, for figures
*net install binscatter2, from("https://raw.githubusercontent.com/mdroste/stata-binscatter2/master/") //needed for some figures

*scheme for figures
set scheme stmono1

**********
*Bias and Quality Estimation - linear trends for each newspaper
**********
use "$mainPath/all_endorsements_JOP.dta", clear

bysort member: egen temp = count(member)
drop if temp<10
drop temp
reghdfe endorseD, absorb(member member#c.time election_g, savefe)
keep if e(sample)
rename __hdfe1__ fe_partisanship
replace fe_partisanship = fe_partisanship+_b[_cons]
replace fe_partisanship = fe_partisanship+(time*__hdfe2__Slope1)
rename __hdfe2__Slope1 fe_partisantrend
rename __hdfe3__ fe_election

*standardize to have unit SD
*fe_p - this is the newspaper-level partisan bias measure "fe_partisanship"
egen tag = tag(member year)
tab tag
summ fe_partisanship if tag==1, d
replace fe_partisanship = fe_partisanship-0.5 //relative to a neutral paper
summ fe_partisanship if tag==1, d
replace fe_partisanship = fe_partisanship/r(sd) //unit std dev
drop tag

*fe_e - this is the election-level quality differential "fe_election"
egen tag = tag(election_g)
tab tag
summ fe_election if tag==1, d
replace fe_election = fe_election/r(sd) //unit std dev
drop tag


/////////////////////////////////////////////////////
**********
*TABLE 1
**********
*create open seat vs incumbent running & endorsed variables
gen openseat = 0
replace openseat = 1 if d_inc==0&r_inc==0
gen incumbents = 0
replace incumbents = 1 if endorsement==1&incumbency==1
replace incumbents = 1 if endorsement==-1&incumbency==-1
replace incumbents = . if openseat==1|(d_inc==.&r_inc==.)

*get unique papers by office type
preserve
keep office_simple member
duplicates drop
gen n_papers = 1
collapse (sum) n_papers, by(office_simple)
tempfile papern
save `papern'
restore
preserve
replace office_simple="Total"
keep office_simple member
duplicates drop
gen n_papers = 1
collapse (sum) n_papers, by(office_simple)
tempfile paperntotal
save `paperntotal'
restore

*unique elections
preserve
gen n_elections = 1
keep election_g n_elections office_simple
duplicates drop
collapse (sum) n_elections, by(office_simple)
tempfile electn
save `electn'
replace office_simple="Total"
collapse (sum) n_elections, by (office_simple)
append using `electn'
tempfile electtotal
save `electtotal'
restore

preserve
gen n_endorsements=1
replace office_simple = "Total"
replace endorseD = 0 if endorseD == 0.5
collapse (mean) endorseD d_inc r_inc openseat incumbents (sum) n_endorsements, by(office_simple)
summ n_endorsements, d
local totends = r(mean)
tempfile endorsetotal
save `endorsetotal'
restore

preserve
gen n_endorsements=1
replace endorseD = 0 if endorseD == 0.5
collapse (mean) endorseD d_inc r_inc openseat incumbents (sum) n_endorsements, by(office_simple)
merge 1:1 office_simple using `papern', nogen
append using `endorsetotal'
merge 1:1 office_simple using `paperntotal', update replace nogen
merge 1:1 office_simple using `electtotal', update replace nogen

gen sampleshare = n_endorsements/`totends'
gen sortingvar = .
replace sortingvar = 1 if office_simple=="President"
replace sortingvar = 2 if office_simple=="US Senate"
replace sortingvar = 3 if office_simple=="US House"
replace sortingvar = 4 if office_simple=="Statewide"
replace sortingvar = 5 if office_simple=="State Legislature"
replace sortingvar = 6 if office_simple=="Local Govt"
replace sortingvar = 7 if office_simple=="Total"
sort sortingvar

**********
*TABLE 1 - Newspaper Endorsement Summary Statistics
**********
local N = _N
local N1 = `N'-1
quietly {

  //log using "$mainPath/table1.tex", text replace
 
 
 noisily display "\begin{tabular}{lcccccc}\hline"
  noisily display "Office & N & ~Share~ & \% Dem & \% Incs & N Papers & N Elections \\"
   noisily display "\hline"

     forvalues i = 1(1)`N1' {
      noisily display office_simple[`i'] " & " %5.0fc n_endorsements[`i'] " & " %5.2f sampleshare[`i'] " & " %5.2f endorseD[`i'] " & " %5.2f incumbents[`i'] " & " %5.0f n_papers[`i'] " & " %5.0fc n_elections[`i'] " \\"
   }
  
   noisily display "\hline"
   noisily display office_simple[`N'] " & " %6.0fc n_endorsements[`N'] " & " %5.2f sampleshare[`N'] " & " %5.2f endorseD[`N'] " & " %5.2f incumbents[`N'] " & " %5.0f n_papers[`N'] " & " %5.0fc n_elections[`N'] " \\"

  noisily display "\end{tabular}   "

  //log close

}
restore

**********
*TABLE 1 - display in stata
**********
tab office_simple
tab office_simple endorseD, row
tab office_simple incumbents, row
egen tag = tag(member)
tab tag
drop tag
egen tag = tag(member office_simple)
tab office_simple tag
drop tag
egen tag = tag(election_g)
tab office_simple tag
drop tag
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
*export "results" files, one is quality estimates and the other is partisan slant estimates 
preserve
keep if office=="PRESIDENT"
keep year office election_g fe_election
gen n_endorsements = 1
collapse (mean) fe_election (sum) n_endorsements, by(year office election_g)
gen state_abbrev="PRES"
tempfile presqual
save `presqual'
restore

preserve
*save quality estimates
gen n_endorsements = 1
drop if office=="PRESIDENT"
drop if office_simple=="Local Govt"|office_simple=="President"
collapse (mean) fe_election (sum) n_endorsements, by(state_abbrev year office dist election_g)
append using `presqual'
rename fe_election quality_differential
label variable quality_differential "Quality Differential"
save "$mainPath/quality_estimates.dta", replace
restore

*partisan bias estimates
preserve
keep state_newspaper newspaper member year fe_partisanship
gen n_endorsements=1
collapse (sum) n_endorsements (first) state_newspaper newspaper fe_partisanship, by(member year)
duplicates drop member year, force
sort state_newspaper member year
order state_newspaper newspaper member year fe_partisanship n_endorsements
rename fe_partisanship newspaper_bias
label variable newspaper_bias "Endorsement-Based Partisan Bias"
compress
save "$mainPath/bias_estimates.dta", replace
restore

compress
save "$mainPath/working_endorsements_westimates.dta", replace
/////////////////////////////////////////////////////




/////////////////////////////////////////////////////
**********
*TABLE 2 - Predicting Newspaper Endorsements
**********

*start with endorsement data
use "$mainPath/all_endorsements_JOP.dta", clear

*merge to county-level presidential results
merge m:1 countyfips year using "$mainPath/pres_county.dta", nogen keep(1 3)
rename d2pshare_pres dshare

*create dem spending advantage variable here
gen D_spend_adv = cfspending_D-cfspending_R
replace D_spend_adv = ln(D_spend_adv)

*create difference-in variables here, for any experience and years of experience, when available
gen exp_years = exp_years_D-exp_years_R

gen temp = 0
replace temp = 1 if anyexp==1&party=="D"
bysort election_g: egen temp2 = max(temp)
drop temp 
gen temp = 0
replace temp = 1 if anyexp==1&party=="R"
bysort election_g: egen temp3 = max(temp)
replace anyexp = temp2-temp3
drop temp*

label variable incumbency "Incumbency"
label variable scandal "Scandal"
label variable anyexp "Prior Experience"
label variable dshare "Dem Vote Share"
label variable D_spend_adv "ln(Relative Spending)"

gen exp_decades = exp_years/10
gen exp_decades2 = exp_decades*exp_decades
replace exp_decades2 = 0-exp_decades2 if endorsement==-1
label variable exp_decades "Decades of Exp."
label variable exp_decades2 "(Decades of Exp.)^2"

drop if office_simple=="Local Govt" 

*save temp data of characteristic, at the election level, for some appendix results
preserve
keep if party_s=="D"
keep state_abbrev year office dist incumbency anyexp exp_decades exp_decades2 scandal D_spend_adv
duplicates drop 
duplicates tag state_abbrev year office dist, gen(tag)
drop if tag!=0 
save "$mainPath/candidate_chars_differences.dta", replace
restore
 
*Table 2
reg endorseD dshare, r
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) replace label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, No)
reghdfe endorseD dshare, a(member)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, \checkmark)
reghdfe endorseD dshare incumbency, a(member)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, \checkmark)
reghdfe endorseD dshare incumbency anyexp if year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(member)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, \checkmark)
reghdfe endorseD dshare incumbency anyexp exp_decades exp_decades2 if year>=1980&(office=="H"|office=="S"|office=="PRESIDENT"|office=="ST H"|office=="ST S"), a(member)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, \checkmark)
reghdfe endorseD dshare incumbency anyexp exp_decades exp_decades2 scandal if (year>=1978&year<=2018&office=="H")|(year>=1978&year<=2018&office=="S"), a(member)
local wr2 = (e(r2_a_within))
local wr2 : display %9.3f `wr2'
outreg2 using "$mainPath/table2.tex", tex(frag) label keep(dshare incumbency anyexp exp_decades exp_decades2 scandal) adjr2 nocons dec(3) nonotes ctitle("Endorse D") addtext(Adjusted Within R^2, `wr2', Newspaper FEs, \checkmark)
/////////////////////////////////////////////////////






/////////////////////////////////////////////////////
*********
*Figure 2 - Partisan Bias of Local Newspapers, 1950s and 2010s
*********
graph drop _all
use "$mainPath/bias_estimates.dta", clear

*figure of changes in news bias over time, 1950-2000s
gen decade = 1950 if year>1950&year<=1960
replace decade = 1960 if year>1960&year<=1970
replace decade = 1970 if year>1970&year<=1980
replace decade = 1980 if year>1980&year<=1990
replace decade = 1990 if year>1990&year<=2000
replace decade = 2000 if year>2000&year<=2010
replace decade = 2010 if year>2010&year<=2020
*kdensity of partisanship of papers, beginning vs end period
*show how 1 or 2 papers change too (add labels - DMN and NYT, and maybe a super liberal paper too?)
kdensity newspaper_bias if decade==1950&newspaper_bias<2.5&newspaper_bias>-2.5, addplot(kdensity newspaper_bias if decade==2010&newspaper_bias<2.5&newspaper_bias>-2.5) legend(on pos(6) rows(1) label(1 "1950s") label(2 "2010s")) xtitle("Estimated Newspaper Partisan Bias (Pro-Dem Bias)", size(4)) title("") xlab(-3(1)3) name(partisandist_50to10)
graph export "$mainPath/figure2.pdf", width(6.5) replace
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
*********
*Figure 3 - Comparisons to Alternative Bias Measures
*********
preserve
use "$mainPath/bias_estimates.dta", clear
replace year = 2006 if year>=2000&year<=2006
keep if year==2006
replace newspaper_bias = newspaper_bias*-1
collapse (mean) newspaper_bias, by(member year)
merge 1:m member year using "$mainPath/bias_compiled.dta", nogen keep(1 3)
keep if source=="Gentzkow and Shapiro, 2010"
reg raw_bias newspaper_bias if year==2006
corr raw_bias newspaper_bias if year==2006
local bias_rho = r(rho)
local bias_rho : display %9.2f `bias_rho'
graph twoway (scatter raw_bias newspaper_bias if year==2006, msymbol(O) mcolor(gray)) (lfit raw_bias newspaper_bias if year==2006), legend(off) ytitle("Gentzkow and Shapiro (2010)", size(4)) xtitle("Endorsement-Based Partisan Bias Measure", size(4)) text(0.35 1.5 "𝝆=`bias_rho'") subtitle("(A) Gentzkow and Shapiro (2010) Comparison", size(4)) name(bias1)
restore

preserve
use "$mainPath/bias_estimates.dta", clear
replace year = 1994 if year>=1994&year<=2004
keep if year==1994
replace newspaper_bias = newspaper_bias*-1
collapse (mean) newspaper_bias, by(member year)
merge 1:m member year using "$mainPath/bias_compiled.dta", nogen keep(1 3)
keep if source=="Ho and Quinn, 2008"
reg raw_bias newspaper_bias if year==1994
corr raw_bias newspaper_bias if year==1994
local bias_rho = r(rho)
local bias_rho : display %9.2f `bias_rho'
graph twoway (scatter raw_bias newspaper_bias if year==1994, msymbol(O) mcolor(gray)) (lfit raw_bias newspaper_bias if year==1994), legend(off) ytitle("Ho and Quinn (2008)", size(4)) xtitle("Endorsement-Based Partisan Bias Measure", size(4)) text(-1.5 1.5 "𝝆=`bias_rho'") subtitle("(B) Ho and Quinn (2008) Comparison", size(4)) name(bias2)
restore

preserve
use "$mainPath/bias_estimates.dta", clear
replace year = 2004 if year>=1996&year<=2012
keep if year==2004
replace newspaper_bias = newspaper_bias*-1
collapse (mean) newspaper_bias, by(member year)
merge 1:m member year using "$mainPath/bias_compiled.dta", nogen keep(1 3)
keep if source=="Puglisi and Snyder, 2015 (group-right of median)"
reg raw_bias newspaper_bias if year==2004
corr raw_bias newspaper_bias if year==2004
local bias_rho = r(rho)
local bias_rho : display %9.2f `bias_rho'
graph twoway (scatter raw_bias newspaper_bias if year==2004, msymbol(O) mcolor(gray)) (lfit raw_bias newspaper_bias if year==2004), legend(off) ytitle("Puglisi and Snyder (2015)", size(4)) xtitle("Endorsement-Based Partisan Bias Measure", size(4)) subtitle("(C) Puglisi and Snyder (2015) Comparison", size(4)) name(bias3) text(-0.75 1.5 "𝝆=`bias_rho'") 
restore

graph combine bias1 bias2 bias3, xcomm rows(2)
graph export "$mainPath/figure3.pdf", width(6.5) replace
/////////////////////////////////////////////////////




/////////////////////////////////////////////////////
*********
*Figure 4 - Comparison to Informant Ratings of Candidate Qualifications
*********
graph drop _all
use "$mainPath/quality_estimates.dta", clear
merge 1:m state_abbrev office dist year using "$mainPath/UCD_CES.dta", nogen keep(3)

bysort election_g: egen nratings = count(election_g)

egen dtot = rowmean(dinteg_pmn dworkoth_pmn dcomp_pmn dgrasp_pmn dsolve_pmn dqualif_pmn dserv_pmn)
egen rtot = rowmean(rinteg_pmn rworkoth_pmn rcomp_pmn rgrasp_pmn rsolve_pmn rqualif_pmn rserv_pmn)

gen delta_integ_pmn = dinteg_pmn-rinteg_pmn
gen delta_workoth_pmn = dworkoth_pmn-rworkoth_pmn
gen delta_comp_pmn = dcomp_pmn-rcomp_pmn
gen delta_grasp_pmn = dgrasp_pmn-rgrasp_pmn
gen delta_solve_pmn = dsolve_pmn-rsolve_pmn
gen delta_qualif_pmn = dqualif_pmn-rqualif_pmn
gen delta_serv_pmn = dserv_pmn-rserv_pmn

collapse (mean) dtot rtot *_pmn, by(election_g state_abbrev office dist year quality_differential nratings)

gen weight = nratings

gen charvalence = dtot-rtot
summ charvalence, d
replace charvalence = charvalence/r(sd)

label variable delta_integ_pmn "Personal Integrity"
label variable delta_workoth_pmn "Works with Other Leaders"
label variable delta_comp_pmn "Competency"
label variable delta_grasp_pmn "Grasp of the Issues"
label variable delta_solve_pmn "Ability to Solve Problems"
label variable delta_qualif_pmn "Qualified for Office"
label variable delta_serv_pmn "Strength as Public Servant"
label variable charvalence "Character Valence"

reg quality_differential delta_* [aw=weight]
local qual_beta = _b[delta_qualif_pmn]
local qual_beta : display %9.3f `qual_beta'
avplot delta_qualif_pmn, xtitle("Informant Opinion - Candidate Qualified for Office", size(4)) ytitle("Endorsement Quality Differential", size(4)) legend(off) text(-1.5 3 " β=`qual_beta'***") xlab(-4(2)4) ylab(-2.5(1)1.5) name(qualified) note("")

reg quality_differential charvalence [aw=weight]
local qual_beta = _b[charvalence]
local qual_beta : display %9.3f `qual_beta'
graph twoway (scatter quality_differential charvalence) (lfit quality_differential charvalence), xtitle("Informant Opinion - Relative Character Valence", size(4)) ytitle("Endorsement Quality Differential", size(4)) legend(off) text(-1.5 2 " β=`qual_beta'") xlab(-3(1.5)3) ylab(-2.5(1)2.5) name(charvalence2)

graph combine charvalence2 qualified, ycomm row(1)
graph export "$mainPath/figure4.pdf", width(6) replace
/////////////////////////////////////////////////////





/////////////////////////////////////////////////////
*********
*Table 3 - Quality Differentials and Governing Effectiveness
*********
*Legislative Effectiveness Scores
graph drop _all
use "$mainPath/quality_estimates.dta", clear
keep if office=="H" 

*merge to LES
merge 1:1 state_abbrev office year dist using "$mainPath/LES.dta", keep(3) nogen

replace quality_differential=quality_differential*-1 if party=="R"

label variable quality_differential "Quality Differential"

reg les quality_differential [aw=n_endorsements]
summ les if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/table3.tex", tex(frag) label replace keep(quality_differential) addstat("Mean of Dependent Var", `depvarmean', "SD of Dependent Var", `depvarstd') adjr2 nocons dec(3) nonotes ctitle("~~~LES Scores~~~") addtext(Party-State FEs, No)


*Approval Ratings
use "$mainPath/quality_estimates.dta", clear
keep if office=="G"|office=="S"
drop dist 

*merge to election results
merge 1:m year state_abbrev office using "$mainPath/masterelections.dta", nogen keep(3)

*merge to approval ratings
merge 1:1 year state_abbrev office using "$mainPath/JARSapprovals.dta", nogen keep(1 3)

replace win_party="R" if win_party=="RF"
replace quality_differential = quality_differential*-1 if win_party=="R"

label variable quality_differential "Quality Differential"

drop party
encode win_party, gen(party)
egen state_party = group(party state_fips)

reghdfe netpos quality_differential [aw=n_endorsements], a(state_party)
summ netpos if e(sample)
local depvarmean = r(mean)
local depvarstd = r(sd)
display `depvarmean'
display `depvarstd'
sum quality_differential if e(sample)
local indepvarmean = r(mean)
local indepvarstd = r(sd)
display `indepvarmean'
display `indepvarstd'
outreg2 using "$mainPath/table3.tex", tex(frag) label keep(quality_differential) addstat("Mean of Dependent Var", `depvarmean', "SD of Dependent Var", `depvarstd') adjr2 nocons dec(3) nonotes ctitle("~~Net Approval~~") addtext(Party-State FEs, \checkmark)
/////////////////////////////////////////////////////





/////////////////////////////////////////////////////
*********
*Figure 5 - Partisan Bias and Quality Differentials Adjusting for Extremity - panels a and b
*********
use "$mainPath/all_endorsements_JOP.dta", clear
graph drop _all

*flip all CF scores - so that postive = more democratic, rather than the other way around:
foreach var of varlist cfscoredyn_D cfscoredyn_R{
	replace `var' = -1*`var'
}

*average of each papers' endorsed candidates CF scores by decade
bysort member decade: gen temp = cfscoredyn_D if endorsement==1
bysort member decade: gen temp2 = cfscoredyn_R if endorsement==-1
bysort member decade: gen temp3 = (cfscoredyn_D+cfscoredyn_R)/2 if endorsement==0
gen avg_score = temp
replace avg_score = temp2 if temp==.
replace avg_score = temp3 if avg_score==.&nonendorsed==1
drop temp*
bysort member decade: egen newspaper_in_cf = mean(avg_score)
bysort member decade: egen n_endorsements_cf = count(avg_score)
drop avg_score*
egen tag = tag(member decade)
summ newspaper_in_cf if tag==1, d
replace newspaper_in_cf = newspaper_in_cf/r(sd)
drop tag 

*DW Nominate
bysort member decade: gen temp = D_dim1score if endorsement==1
bysort member decade: gen temp2 = R_dim1score if endorsement==-1
bysort member decade: gen temp3 = (D_dim1score+R_dim1score)/2 if endorsement==0
gen avg_score = temp
replace avg_score = temp2 if temp==.
replace avg_score = temp3 if avg_score==.&nonendorsed==1
drop temp*
bysort member decade: egen newspaper_in_dw = mean(avg_score)
bysort member decade: egen n_endorsements_dw = count(avg_score)
drop avg_score*
egen tag = tag(member decade)
summ newspaper_in_dw if tag==1, d
replace newspaper_in_dw = newspaper_in_dw/r(sd)
drop tag

*create a squared difference in the distance variable, as in the model - CF
gen dist_D_cf = abs(newspaper_in_cf-cfscoredyn_D)
replace dist_D_cf = dist_D_cf*dist_D_cf
gen dist_R_cf = abs(newspaper_in_cf-cfscoredyn_R)
replace dist_R_cf = dist_R_cf*dist_R_cf
gen dist_diff2_cf = dist_D_cf-dist_R_cf

*create a squared difference in the distance variable, as in the model - DW
gen dist_D_dw = abs(newspaper_in_dw-D_dim1score)
replace dist_D_dw = dist_D_dw*dist_D_dw
gen dist_R_dw = abs(newspaper_in_dw-R_dim1score)
replace dist_R_dw = dist_R_dw*dist_R_dw
gen dist_diff2_dw = dist_D_dw-dist_R_dw


*tempfile for later
save "$mainPath/workingendorements_temp.dta", replace


*add new estimates onto original slant estimates, save copies
*CF
preserve
keep member decade newspaper_in_cf n_endorsements_cf
duplicates drop
drop if n_endorsements_cf<=10
*interpolate missing year
gen year=decade
replace year = year+5
tsset member year, delta(1)
tsfill
bysort member: ipolate newspaper_in_cf year, gen(newspaper_bias_cf)
drop newspaper_in_cf n_endorsements_cf decade
drop if mod(year,2)!=0
save "$mainPath/bias_estimates_cf.dta", replace
restore

*DW
preserve
keep member decade newspaper_in_dw n_endorsements_dw
duplicates drop
drop if n_endorsements_dw<=10
*interpolate missing year
gen year=decade
replace year = year+5
tsset member year, delta(1)
tsfill
bysort member: ipolate newspaper_in_dw year, gen(newspaper_bias_dw)
drop newspaper_in_dw n_endorsements_dw decade
drop if mod(year,2)!=0
save "$mainPath/bias_estimates_dw.dta", replace
restore


preserve
use "$mainPath/bias_estimates.dta", clear
merge 1:1 member year using "$mainPath/bias_estimates_dw.dta", nogen
merge 1:1 member year using "$mainPath/bias_estimates_cf.dta", nogen

// // // // // later, merge to cleaned newspaper ID spreadsheet for all the right ID variables:
// // // // // merge 1:1 member using ...
sort state_newspaper member year newspaper 
order  state_newspaper member year newspaper newspaper_bias newspaper_bias_dw newspaper_bias_cf
label variable newspaper_bias_dw "Bias (DW-Nominate)"
label variable newspaper_bias_cf "Bias (CF Scores)"

compress
save "$mainPath/bias_estimates_walternatives.dta", replace
restore


*now comparing partisanship estimates across overlapping data
merge m:1 member year using "$mainPath/bias_estimates.dta"

*calculate average endorsement-based partisan measure across all endorsements, by decade
bysort member decade: egen newspaper_bias_avg = mean(newspaper_bias)

*compare endorsement-based bias and cfscore-based bias averages over whole time period
preserve
keep newspaper_in_cf newspaper_bias_avg member decade n_endorsements_cf
drop if n_endorsements_cf<10
duplicates drop 
reg newspaper_in_cf newspaper_bias_avg [aw=n_endorsements_cf]
corr newspaper_in_cf newspaper_bias_avg [aw=n_endorsements_cf]
local fe_in_rho = round(r(rho),0.001)
local fe_in_rho : display %9.3f `fe_in_rho'
display `fe_in_rho'
local nendorsements = r(N)
graph twoway (scatter newspaper_in_cf newspaper_bias_avg [aw=n_endorsements_cf], msize(tiny) msymbol(circle_hollow))  (lfit newspaper_in_cf newspaper_bias_avg [aw=n_endorsements_cf]), ytitle("Mean CF Score of Endorsed", size(4)) xtitle("Endorsement-Based Partisan Bias", size(4)) title("(A) CF-Based Partisan Bias") xlab(-4(1)4) text(1.5 -3 "𝝆=`fe_in_rho'") legend(off) name(partisancorr_altversion_cf) 
restore

*compare endorsement-based bias and DW-nominate bias averages over whole time period
preserve
keep newspaper_in_dw newspaper_bias_avg member decade n_endorsements_dw
drop if n_endorsements_dw<10
duplicates drop 
reg newspaper_in_dw newspaper_bias_avg [aw=n_endorsements_dw]
corr newspaper_in_dw newspaper_bias_avg [aw=n_endorsements_dw]
local fe_in_rho = round(r(rho),0.001)
local fe_in_rho : display %9.3f `fe_in_rho'
display `fe_in_rho'
local nendorsements = r(N)
graph twoway (scatter newspaper_in_dw newspaper_bias_avg [aw=n_endorsements_dw], msize(tiny) msymbol(circle_hollow))  (lfit newspaper_in_dw newspaper_bias_avg [aw=n_endorsements_dw]), ytitle("Mean DW-Nom Scores of Endorsed", size(4)) xtitle("Endorsement-Based Partisan Bias", size(4)) title("(B) DW-Nominate Partisan Bias") xlab(-4(1)4) text(1.5 -3 "𝝆=`fe_in_rho'") legend(off) name(partisancorr_altversion_dw) 
restore



*********
*Figure 5 - Partisan Bias and Quality Differentials Adjusting for Extremity - panels c and d
*********
*next, compare quality estimates, using different ideological scores
use  "$mainPath/workingendorements_temp.dta", clear

label variable dist_diff2_cf "$(i_n-i_D)^2-(i_n-i_R)^2$"
label variable dist_diff2_dw "$(i_n-i_D)^2-(i_n-i_R)^2$"
label variable newspaper_in_cf "Newspaper Ideology"
label variable newspaper_in_dw "Newspaper Ideology"

rename dist_diff2_cf dist_diff2
rename newspaper_in_cf newspaper_in

**quality differential_cf**
*quality differential from endorsements while taking into account ideology (CF Scores)
reghdfe endorseD dist_diff2, absorb(election_g, savefe)
rename __hdfe1__ quality_differential_cf
*standardize, like the other measures
egen tag = tag(election_g) if e(sample)
summ quality_differential_cf if tag==1, d
replace quality_differential_cf = quality_differential_cf/r(sd)
drop tag 

rename newspaper_in newspaper_in_cf
rename newspaper_in_dw newspaper_in
rename dist_diff2 dist_diff2_cf
rename dist_diff2_dw dist_diff2

**quality differential_dw**
*quality differential from endorsements while taking into account ideology (DW Nom Scores)
reghdfe endorseD dist_diff2, absorb(election_g, savefe)
rename __hdfe1__ quality_differential_dw
*standardize, like the other measures
egen tag = tag(election_g) if e(sample)
summ quality_differential_dw if tag==1, d
replace quality_differential_dw = quality_differential_dw/r(sd)
drop tag

rename dist_diff2 dist_diff2_dw
//rename newspaper_in newspaper_in_dw 

keep election_g quality_differential_cf quality_differential_dw
duplicates drop 
drop if quality_differential_cf==.&quality_differential_dw==.

merge 1:1 election_g using "$mainPath/quality_estimates.dta", keep(2 3)
order quality_differential_cf quality_differential_dw, after(quality_differential)
drop _merge 
sort state_abbrev office year dist 

label variable quality_differential_cf "Quality Differential - CF Adjusted"
label variable quality_differential_dw "Quality Differential - DW-Nominate Adjusted"

compress
replace dist = 0 if office=="S"
save "$mainPath/quality_estimates_walternatives.dta", replace

use "$mainPath/quality_estimates_walternatives.dta", clear

preserve
keep election_g quality_differential quality_differential_cf n_endorsements
keep if quality_differential_cf!=.&quality_differential!=.
duplicates drop 
reg quality_differential_cf quality_differential [aw=n_endorsements]
corr quality_differential_cf quality_differential [aw=n_endorsements]
local fe_in_rho = round(r(rho),0.001)
local fe_in_rho : display %9.3f `fe_in_rho'
local n_obs = r(N)
display `fe_in_rho'
display `n_obs'
graph twoway (scatter quality_differential_cf quality_differential [aw=n_endorsements], msize(tiny) msymbol(circle_hollow)) (lfit quality_differential_cf quality_differential), legend(off) ytitle("CF-Adjusted Quality Differential", size(4)) xtitle("Quality Differential", size(4)) title("(C) CF-Adjusted Quality Differential", size(4)) text(0.75 -2.25 "𝝆=`fe_in_rho'") xlab(-3(1)3) name(qcompare_cf) //text(-0.39 1.2 "N=`n_obs'") 
restore

preserve
keep election_g quality_differential quality_differential_dw n_endorsements
keep if quality_differential_dw!=.&quality_differential!=.
duplicates drop 
reg quality_differential_dw quality_differential [aw=n_endorsements]
corr quality_differential_dw quality_differential [aw=n_endorsements] 
local fe_in_rho = round(r(rho),0.001)
local fe_in_rho : display %9.3f `fe_in_rho'
local n_obs = r(N)
display `fe_in_rho'
display `n_obs'
graph twoway (scatter quality_differential_dw quality_differential [aw=n_endorsements], msize(tiny) msymbol(circle_hollow)) (lfit quality_differential_dw quality_differential), legend(off) ytitle("DW-Nom Adjusted Quality Differential", size(4)) xtitle("Quality Differential", size(4)) title("(D) DW-Nominate-Adjusted Quality Differential", size(4)) text(0.4 -2.25 "𝝆=`fe_in_rho'") xlab(-3(1)3) name(qcompare_dw) //text(-0.39 1.2 "N=`n_obs'")
restore

*FIGURE 5 - combined panels*
graph combine partisancorr_altversion_cf partisancorr_altversion_dw qcompare_cf qcompare_dw, rows(2)
graph export "$mainPath/figure5.pdf", width(6) replace
/////////////////////////////////////////////////////




/////////////////////////////////////////////////////
**********
*Figure 6 - Incumbent Quality and Electoral Success
**********
graph drop _all 
use "$mainPath/quality_estimates.dta", clear
*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

*flip so that quality estimates are relative to challenger 
replace quality_differential = quality_differential*-1 if incumbency==-1
gen inc_win = 0 
replace inc_win = 1 if win_party=="D"&incumbency==1
replace inc_win = 1 if win_party=="R"&incumbency==-1
replace inc_win = . if incumbency==.|win_party==""|incumbency==0

drop if quality_differential<-2|quality_differential>2
kdensity quality_differential if inc_win==1, addplot(kdensity quality_differential if inc_win==0) legend(on row(1) pos(6) label(1 "Incumbent Wins") label(2 "Incumbent Loses")) xtitle("Quality of Incumbent Relative to Challenger", size(4)) title("")
graph export "$mainPath/figure6.pdf", width(6) replace

*incumbent and quality stats:
count if (incumbency==1|incumbency==-1)
count if (incumbency==1|incumbency==-1)&quality_differential>0
count if (incumbency==1|incumbency==-1)&quality_differential<0

count if (incumbency==1|incumbency==-1)&inc_win==1
count if (incumbency==1|incumbency==-1)&inc_win==1&quality_differential>0
count if (incumbency==1|incumbency==-1)&inc_win==1&quality_differential<0

count if (incumbency==1|incumbency==-1)&inc_win==0
count if (incumbency==1|incumbency==-1)&inc_win==0&quality_differential>0
count if (incumbency==1|incumbency==-1)&inc_win==0&quality_differential<0
/////////////////////////////////////////////////////




/////////////////////////////////////////////////////
**********
*Figure 7 - Vote Shares, Probability of Winning, and Quality Differentials
**********
graph drop _all 
use "$mainPath/quality_estimates.dta", clear
*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

binscatter2 d2pshare quality_differential if d2pshare!=0&d2pshare!=1&quality_differential>=-2&quality_differential<=2, linetype(lfit) nquantiles(50) xtitle("Candidate Quality Differential", size(4.5)) xlab(-2(0.5)2) ylab(0.3(0.1)0.7) ytitle("Democratic Two-Party Vote Share", size(4.5)) name(voteshareqdiffs) subtitle("(a) Vote Shares") 

graph twoway lpolyci pdwin quality_differential if d2pshare!=0&d2pshare!=1&quality_differential>=-2&quality_differential<=2, xtitle("Candidate Quality Differential", size(4.5)) xlab(-2(0.5)2) ytitle("P(D Win)", size(4.5)) name(pdwinqdiffs) legend(off) ylab(0(0.25)1) subtitle("(b) Probability of Winning") 

graph combine voteshareqdiffs pdwinqdiffs, xcomm
graph export "$mainPath/figure7.pdf", width(6) replace
/////////////////////////////////////////////////////



/////////////////////////////////////////////////////
**********
*Table 4 - Effects of Quality Differentials on Vote Shares
**********
graph drop _all 
use "$mainPath/quality_estimates.dta", clear

*merge to election data 
merge 1:1 state_abbrev year office dist using "$mainPath/masterelections.dta",  keep(1 3)

gen pdwin = .
replace pdwin = d2pshare>0.5 if d2pshare!=0&d2pshare!=1&d2pshare!=.

label variable quality_differential "Quality Differential"
label variable incumbency "Incumbency"

*dshare
reghdfe d2pshare quality_differential if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label replace keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe d2pshare quality_differential incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("D Vote") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

*pwin
reghdfe pdwin quality_differential if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)

reghdfe pdwin quality_differential incumbency if d2pshare!=0&d2pshare!=1, a(constituencyFE year)
outreg2 using "$mainPath/table4.tex", tex(frag) label keep(quality_differential incumbency) adjr2 nocons dec(3) nonotes ctitle("P(D Win)") addtext(Year FE, \checkmark, Constituency FE, \checkmark)
/////////////////////////////////////////////////////










